package com.lovo.emp.frame;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.util.ArrayList;

import javax.swing.*;

import com.lovo.common.model.DeptBean;
import com.lovo.common.model.EmpBean;
import com.lovo.common.model.WorkBean;
import com.lovo.common.service.ISysDictService;
import com.lovo.common.service.impl.SysDictServiceImpl;
import com.lovo.dept.dao.IDeptDao;
import com.lovo.dept.service.IDeptService;
import com.lovo.dept.service.impl.DeptServiceImpl;
import com.lovo.emp.service.IEmpService;
import com.lovo.emp.service.impl.EmpServiceImpl;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoImageLabel;
import com.lovo.netCRM.component.LovoLabel;
import com.lovo.netCRM.component.LovoTitleLabel;
import com.lovo.netCRM.component.LovoTxt;

/**
 * 员工修改对话框
 * @author 白德柯
 *
 */
public class EmpUpdateDialog extends JDialog{

	/**员工主面板*/
	private EmpPanel empPanel;
	/**员工ID*/
	private int id;
	/**姓名标签*/
	private LovoLabel nameLabel = new LovoLabel("姓    名",40,90,this);
	/**性别标签*/
	private LovoLabel genderLabel = new LovoLabel("性    别",300,90,this);
	/**登录名标签*/
	private LovoLabel loginNameLabel = new LovoLabel("登录名",40,130,this);
	/**文化程度标签*/
	private LovoLabel educationLabel = new LovoLabel("文化程度",300,130,this);
	/**出生日期标签*/
	private LovoLabel bornLabel = new LovoLabel("出生日期",40,170,this);
	/**联系方式标签*/
	private LovoTxt phoneTxt = new LovoTxt("联系方式",300,170,this);
	/**所属专业标签*/
	private LovoLabel subjectLabel = new LovoLabel("所属专业",40,210,this);
	/**政治面貌标签*/
	private LovoTxt politicalTxt = new LovoTxt("政治面貌",300,210,this);
	/**家庭地址标签*/
	private LovoLabel addrLabel = new LovoLabel("家庭地址",40,250,this);
	/**工作职位标签*/
	private LovoComboBox<WorkBean> workComboBox;
	/**所在部门标签*/
	private LovoComboBox<DeptBean> deptComboBox;
	/**入职时间标签*/
	private LovoLabel joinDateLabel = new LovoLabel("入职时间",300,290,this);
	/**头像标签*/
	private LovoImageLabel faceLabel = new LovoImageLabel(550, 90, 100, 100,this);

	private EmpBean empBean;

	private IEmpService empService = new EmpServiceImpl();
	private IDeptService deptService = new DeptServiceImpl();
	
	public EmpUpdateDialog(JFrame jf,int id,EmpPanel empPanel){
		super(jf,true);
		this.empPanel = empPanel;
		this.id = id;
		
		this.setLayout(null);
		
		this.init();
		this.setTitle("员工修改");
		Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) screensize.getWidth() / 2 - 390;
		int y = (int) screensize.getHeight() / 2 - 240;
		this.setBounds(x, y, 780, 480);
		this.setVisible(true);
	}
	/**
	 * 初始化
	 *
	 */
	private void init(){
		LovoTitleLabel titleLabel = new LovoTitleLabel("员工修改",this);
		titleLabel.setLocation(300, titleLabel.getY());
		this.initComboBox();
		this.initData(id);
		LovoButton updateButton = new LovoButton(300,330,"确定","image/update.png",this);
		updateButton.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				boolean isOk = update();
				if(isOk){
					// 重新渲染表格
					empPanel.initData();
					EmpUpdateDialog.this.dispose();
				}else{
					JOptionPane.showMessageDialog(EmpUpdateDialog.this,"修改失败");
				}
			}});
	
	}
	
	
	//-------------------------------------------
	
	/**
	 * 初始化下拉框
	 *
	 */
	private void initComboBox(){
		//添加工作职位List集合
		this.workComboBox = new LovoComboBox("工作职位",new ArrayList(),300,250,this);
		//添加所属部门List集合
		this.deptComboBox = new LovoComboBox("所属部门",deptService.findAll(),40,290,this);
		
	}
	
	/**
	 * 初始化数据
	 * @param id
	 */
	private void initData(int id){
		EmpBean empBean = empService.findById(id);
		nameLabel.setText(empBean.getName());
		genderLabel.setText(empBean.getGender());
		loginNameLabel.setText(empBean.getLoginName());
		educationLabel.setText(empBean.getEducation());
		if(empBean.getBorn() != null){
			bornLabel.setText(empBean.getBorn().toString());
		}
		phoneTxt.setText(empBean.getPhone());
		subjectLabel.setText(empBean.getSubject());
		politicalTxt.setText(empBean.getPolitical());
		addrLabel.setText(empBean.getAddr());

		// 设置部门下拉框中显示为当前员工的所属部门
		deptComboBox.setItem(empBean.getDeptId(),"id");
		if(empBean.getJoinDate() != null){
			joinDateLabel.setText(empBean.getJoinDate().toString());
		}
		faceLabel.setImage(empBean.getFace());

	}
	
	/**
	 * 修改操作
	 *
	 */
	private boolean update(){
		EmpBean empBean = new EmpBean();
		empBean.setId(id);
		empBean.setPhone(phoneTxt.getText());
		empBean.setPolitical(politicalTxt.getText());
		empBean.setDeptId(deptComboBox.getItem().getId());
//		empBean.setWorkId(workComboBox.getItem().getId());
		if(empService.update(empBean) > 0){
			return true;
		}
		return false;
	}

}
